package cn.edu.scau.cmi.wuzewei.hibernate.dao;

import java.util.List;

import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import cn.edu.scau.cmi.wuzewei.hibernate.domain.Teacher;

public class TeacherDAOByHibernate extends BaseHibernateDAO {
	
	public static final String NAME = "name";

	public void save(Teacher transientInstance) {
		try {
			getSession().save(transientInstance);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void delete(Teacher persistentInstance) {
		try {
			getSession().delete(persistentInstance);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public Teacher findById(java.lang.Long id) {
		try {
			Teacher instance = (Teacher) getSession().get("cn.edu.scau.cmi.wuzewei.hibernate.domain.Teacher", id);
			return instance;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	@SuppressWarnings("rawtypes")
	public List findByExample(Teacher instance) {
		try {
			List results = getSession().createCriteria("cn.edu.scau.cmi.wuzewei.hibernate.domain.Teacher")
					.add(Example.create(instance)).list();
			return results;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	@SuppressWarnings("rawtypes")
	public List findByProperty(String propertyName, Object value) {
		try {
			String queryString = "from Teacher as model where model." + propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	@SuppressWarnings("rawtypes")
	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	@SuppressWarnings("rawtypes")
	public List findAll() {
		try {
			String queryString = "from Teacher";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public Teacher merge(Teacher detachedInstance) {
		try {
			Teacher result = (Teacher) getSession().merge(detachedInstance);
			return result;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void attachDirty(Teacher instance) {
		try {
			getSession().saveOrUpdate(instance);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void attachClean(Teacher instance) {
		try {
			getSession().buildLockRequest(LockOptions.NONE).lock(instance);
		} catch (RuntimeException re) {
			throw re;
		}
	}
}
